package com.example.jiradata.service;

import com.example.jiradata.jpa.JiraIssueTypeRepository;
import com.example.jiradata.pojo.JiraIssueType;
import com.example.jiradata.utils.HttpClientUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class JiraIssueTypeService {

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    private JiraIssueTypeRepository jiraIssueTypeRepository;

    public void syncIssueType() throws IOException {
        String res = httpClientUtils.exchange("https://tngasia.atlassian.net/rest/api/2/issuetype",new ParameterizedTypeReference<String>(){},null, HttpMethod.GET);
        System.out.println(res);

        List<JiraIssueType> jiraIssueTypes = objectMapper.readValue(res, new TypeReference<List<JiraIssueType>>() { });
        for (JiraIssueType jiraIssueType : jiraIssueTypes) {
            System.out.println(jiraIssueType.getJiraIssueTypeId());
            jiraIssueTypeRepository.save(jiraIssueType);
        }
    }

}
